package cn.source.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.system.domain.GkGradeDoc;
import cn.source.system.domain.SchoolPlan;
import cn.source.system.domain.SchoolZyzYearAdmission;
import cn.source.system.service.IGkGradeDocService;
import cn.source.system.service.ISchoolPlanService;
import cn.source.system.service.ISchoolZyzYearAdmissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.SchoolMajorYearAdmission;
import cn.source.system.service.ISchoolMajorYearAdmissionService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 年度学校各专业录取情况Controller
 * 
 * @author sourcebyte.vip
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/system/schoolMajorYearAdmission")
public class SchoolMajorYearAdmissionController extends BaseController
{
    @Autowired
    private ISchoolMajorYearAdmissionService schoolMajorYearAdmissionService;

    @Autowired
    private ISchoolPlanService schoolPlanService;

    @Autowired
    ISchoolZyzYearAdmissionService schoolZyzYearAdmissionService;

    @Autowired
    private IGkGradeDocService gkGradeDocService;

    /**
     * 查询年度学校各专业录取情况列表
     */
    //@PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchoolMajorYearAdmission schoolMajorYearAdmission)
    {
        startPage();
        List<SchoolMajorYearAdmission> list = schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionList(schoolMajorYearAdmission);
        return getDataTable(list);
    }
    
    @GetMapping("/checkList")
    public TableDataInfo checkList(SchoolMajorYearAdmission schoolMajorYearAdmission)
    {
        startPage();
        List<SchoolMajorYearAdmission> list = schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionCheckList(schoolMajorYearAdmission);
        return getDataTable(list);
    }

    /**
     * 导出年度学校各专业录取情况列表
     */
    @PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:export')")
    @Log(title = "年度学校各专业录取情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchoolMajorYearAdmission schoolMajorYearAdmission)
    {
        List<SchoolMajorYearAdmission> list = schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionList(schoolMajorYearAdmission);
        ExcelUtil<SchoolMajorYearAdmission> util = new ExcelUtil<SchoolMajorYearAdmission>(SchoolMajorYearAdmission.class);
        util.exportExcel(response, list, "年度学校各专业录取情况数据");
    }

    /**
     * 获取年度学校各专业录取情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolMajorYearAdmissionService.selectSchoolMajorYearAdmissionById(id));
    }

    /**
     * 新增年度学校各专业录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:add')")
    @Log(title = "年度学校各专业录取情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolMajorYearAdmission schoolMajorYearAdmission)
    {

        int i=schoolMajorYearAdmissionService.insertSchoolMajorYearAdmission(schoolMajorYearAdmission);

        return toAjax(i);
    }

    /**
     * 修改年度学校各专业录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:edit')")
    @Log(title = "年度学校各专业录取情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolMajorYearAdmission schoolMajorYearAdmission)
    {
        int returnVal=schoolMajorYearAdmissionService.updateSchoolMajorYearAdmission(schoolMajorYearAdmission);
        //将本条指南同步至计划
        int year=schoolMajorYearAdmission.getYear();
        boolean planExist=false;


        SchoolPlan schoolPlanQuery=new SchoolPlan();
        schoolPlanQuery.setMajorType(schoolMajorYearAdmission.getMajorType());
        schoolPlanQuery.setSchoolCode(schoolMajorYearAdmission.getsCode());
        schoolPlanQuery.setZyz(schoolMajorYearAdmission.getZyz());
        schoolPlanQuery.setMajorName(schoolMajorYearAdmission.getMajor());
        schoolPlanQuery.setYear(year);
        List<SchoolPlan> list2024=schoolPlanService.selectSchoolPlanList2(schoolPlanQuery);

        String province="";
        for(int j=0;j<list2024.size();j++) {
            SchoolPlan schoolPlan2 = list2024.get(j);
            province = schoolPlan2.getSprovince();
        }

        List<GkGradeDoc> gradeList=null;
        if ("450000".equals(province)) {//广西院校，用广西排名

            //广西排位分
            GkGradeDoc gkGradeDoc = new GkGradeDoc();
            gkGradeDoc.setYear(year);
            if ("理工类".equals(schoolMajorYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("工类");
            }
            if ("文史类".equals(schoolMajorYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("史类");
            }
            gkGradeDoc.setUseIn("广西");
            gradeList = gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
        }else{
            //全国排位分
            GkGradeDoc gkGradeDoc=new GkGradeDoc();
            gkGradeDoc.setYear(year);
            if("理工类".equals(schoolMajorYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("工类");
            }
            if("文史类".equals(schoolMajorYearAdmission.getMajorType())) {
                gkGradeDoc.setGradeClass("史类");
            }
            gkGradeDoc.setUseIn("全国");
            gradeList=gkGradeDocService.selectGkGradeDocList(gkGradeDoc);
        }

        for(int j=0;j<list2024.size();j++){
            SchoolPlan schoolPlan2=list2024.get(j);
            province=schoolPlan2.getSprovince();

            planExist=true;

            schoolPlan2.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
            schoolPlan2.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
            schoolPlan2.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
            schoolPlan2.setMajorCode(schoolMajorYearAdmission.getMajorCode());
            if(schoolMajorYearAdmission.getLowGradeSeq()!=null){
                schoolPlan2.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());
            }

            Integer low = schoolMajorYearAdmission.getLowGrade();
            if(low==null) low=0;
            Integer avg = schoolMajorYearAdmission.getAvgGrade();
            if(avg==null) avg=0;


            for (int k = 0; k < gradeList.size(); k++) {
                GkGradeDoc gradeDoc = gradeList.get(k);
                if (gradeDoc.getGrade().intValue() == low.intValue()) {
                    if(schoolPlan2.getMajorLowPm()==null) {
                        schoolPlan2.setMajorLowPm(gradeDoc.getSeq());
                    }
                }
                if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                    schoolPlan2.setMajorAvgPm(gradeDoc.getSeq());
                }
            }


            schoolPlanService.updateSchoolPlan(schoolPlan2);

            //计算专业组汇总数据
            SchoolZyzYearAdmission query=new SchoolZyzYearAdmission();
            query.setYear(year);
            query.setMajorType(schoolPlan2.getMajorType());
            query.setZyz(schoolPlan2.getZyz());
            query.setsCode(schoolPlan2.getSchoolCode());
            List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
            if(zyzList!=null&&zyzList.size()>0){//有现成数据，修改即可
                SchoolZyzYearAdmission zyzYearAdmission=zyzList.get(0);
                schoolPlan2.setZyzPlan(zyzYearAdmission.getPlan());
                schoolPlan2.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                schoolPlan2.setZyzAdmin(zyzYearAdmission.getAdmission());
                schoolPlan2.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                schoolPlan2.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                Integer zyzAvg = schoolPlan2.getZyzAvgGrade();
                if(zyzAvg==null) zyzAvg=0;
                for (int k = 0; k < gradeList.size(); k++) {
                    GkGradeDoc gradeDoc = gradeList.get(k);

                    if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                        schoolPlan2.setZyzAvgPm(gradeDoc.getSeq());
                    }
                }
            }else{//没有现成数据，需要计算

                List<SchoolPlan> listHz=schoolPlanService.selectZyzPlanList(schoolPlan2);
                if(listHz!=null&&listHz.size()>0){
                    SchoolPlan planHz=listHz.get(0);

                    schoolPlan2.setZyzPlan(planHz.getZyzPlan());
                    //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                    schoolPlan2.setZyzAdmin(planHz.getZyzAdmin());
                    schoolPlan2.setZyzLowGrade(planHz.getZyzLowGrade());
                    schoolPlan2.setZyzAvgGrade(planHz.getZyzAvgGrade());

                    Integer zyzAvg = schoolPlan2.getZyzAvgGrade();
                    Integer zyzLow =schoolPlan2.getZyzLowGrade();
                    if(zyzAvg==null) zyzAvg=0;
                    if(zyzLow==null) zyzLow=0;
                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);

                        if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                            schoolPlan2.setZyzAvgPm(gradeDoc.getSeq());
                        }

                        if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                            schoolPlan2.setZyzLowPm(gradeDoc.getSeq());
                        }
                    }
                }
            }

            schoolPlanService.updateSchoolPlan(schoolPlan2);

            break;
        }
        //计划不存在，需要将指南数据插入计划
        if(!planExist){

            List<SchoolPlan> addList=new ArrayList();

            //查找当前专业最新的一条计划信息（主要取专业排名可科目）
            schoolPlanQuery=new SchoolPlan();
            schoolPlanQuery.setMajorType(schoolMajorYearAdmission.getMajorType());
            schoolPlanQuery.setSchoolCode(schoolMajorYearAdmission.getsCode());
            schoolPlanQuery.setZyz(schoolMajorYearAdmission.getZyz());
            schoolPlanQuery.setMajorCode(schoolMajorYearAdmission.getMajorCode());
            List<SchoolPlan> list=schoolPlanService.selectSchoolPlanList2(schoolPlanQuery);
            SchoolPlan plan=null;
            if(list!=null&&list.size()>0){
                plan=list.get(0);
            }
            if(plan!=null) {

                SchoolPlan p = new SchoolPlan();
                p.setYear(year);
                p.setMajorType(schoolMajorYearAdmission.getMajorType());

                p.setZyz(schoolMajorYearAdmission.getZyz());
                p.setSchoolCode(schoolMajorYearAdmission.getsCode());
                p.setSchoolName(plan.getSchoolName());

                p.setMajorCode(plan.getMajorCode());
                p.setMajorName(plan.getMajorName());
                p.setMajorSeq(plan.getMajorSeq());

                p.setKm1(plan.getKm1());
                p.setKm2(plan.getKm2());
                p.setBatch("本科普通批");
                p.setKm21(plan.getKm21());
                p.setKm22(plan.getKm22());
                p.setKm23(plan.getKm23());
                p.setMajorPm(plan.getMajorPm());

                p.setMajorAdmin(schoolMajorYearAdmission.getAdmission());
                p.setMajorPlan(schoolMajorYearAdmission.getPlan());
                p.setMajorLowGrade(schoolMajorYearAdmission.getLowGrade());
                p.setMajorAvgGrade(schoolMajorYearAdmission.getAvgGrade());
                p.setMajorLowPm(schoolMajorYearAdmission.getLowGradeSeq());

                Integer low = schoolMajorYearAdmission.getLowGrade();
                if (low == null) low = 0;
                Integer avg = schoolMajorYearAdmission.getAvgGrade();
                if (avg == null) avg = 0;

                for (int k = 0; k < gradeList.size(); k++) {
                    GkGradeDoc gradeDoc = gradeList.get(k);
                    if (gradeDoc.getGrade().intValue() == low.intValue()) {
                        if (p.getMajorLowPm() == null) {
                            p.setMajorLowPm(gradeDoc.getSeq());
                        }
                    }
                    if (gradeDoc.getGrade().intValue() == avg.intValue()) {
                        p.setMajorAvgPm(gradeDoc.getSeq());
                    }
                }

                addList.add(p);
                schoolPlanService.insertSchoolPlanAllList2(addList);
                addList.clear();

                //计算专业组汇总数据
                SchoolZyzYearAdmission query = new SchoolZyzYearAdmission();
                query.setYear(year);
                query.setMajorType(schoolMajorYearAdmission.getMajorType());
                query.setZyz(schoolMajorYearAdmission.getZyz());
                List<SchoolZyzYearAdmission> zyzList = schoolZyzYearAdmissionService.selectSchoolZyzYearAdmissionList(query);
                if (zyzList != null && zyzList.size() > 0) {//有现成数据，修改即可
                    SchoolZyzYearAdmission zyzYearAdmission = zyzList.get(0);
                    p.setZyzPlan(zyzYearAdmission.getPlan());
                    p.setZyzLowPm(zyzYearAdmission.getLowGradeSeq());
                    p.setZyzAdmin(zyzYearAdmission.getAdmission());
                    p.setZyzLowGrade(zyzYearAdmission.getLowGrade());
                    p.setZyzAvgGrade(zyzYearAdmission.getAvgGrade());

                    Integer zyzAvg = p.getZyzAvgGrade();
                    if (zyzAvg == null) zyzAvg = 0;
                    for (int k = 0; k < gradeList.size(); k++) {
                        GkGradeDoc gradeDoc = gradeList.get(k);

                        if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                            p.setZyzAvgPm(gradeDoc.getSeq());
                        }
                    }
                } else {//没有现成数据，需要计算

                    List<SchoolPlan> listHz = schoolPlanService.selectZyzPlanList(p);
                    if (listHz != null && listHz.size() > 0) {
                        SchoolPlan planHz = listHz.get(0);

                        p.setZyzPlan(planHz.getZyzPlan());
                        //schoolPlan.setZyzLowPm(planHz.getLowGradeSeq());
                        p.setZyzAdmin(planHz.getZyzAdmin());
                        p.setZyzLowGrade(planHz.getZyzLowGrade());
                        p.setZyzAvgGrade(planHz.getZyzAvgGrade());

                        Integer zyzAvg = p.getZyzAvgGrade();
                        Integer zyzLow = p.getZyzLowGrade();
                        if (zyzAvg == null) zyzAvg = 0;
                        if (zyzLow == null) zyzLow = 0;
                        for (int k = 0; k < gradeList.size(); k++) {
                            GkGradeDoc gradeDoc = gradeList.get(k);

                            if (gradeDoc.getGrade().intValue() == zyzAvg.intValue()) {
                                p.setZyzAvgPm(gradeDoc.getSeq());
                            }

                            if (gradeDoc.getGrade().intValue() == zyzLow.intValue()) {
                                p.setZyzLowPm(gradeDoc.getSeq());
                            }
                        }
                    }
                }

                schoolPlanService.updateSchoolPlan(p);
            }
        }


        return toAjax(returnVal);
    }

    /**
     * 删除年度学校各专业录取情况
     */
    @PreAuthorize("@ss.hasPermi('system:schoolMajorYearAdmission:remove')")
    @Log(title = "年度学校各专业录取情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolMajorYearAdmissionService.deleteSchoolMajorYearAdmissionByIds(ids));
    }
}
